﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsTest
{
    /// <summary>
    /// 字符分割类
    /// </summary>
    class CharactersSegmentation
    {
        #region Test

        public static int GetLineHeight(Bitmap bm)
        {
            var im = Common.ToBinary(bm, Matlab.GetThreshold(bm));
            return getLineHeight(im);
        }

        #endregion

        public static List<Rectangle> CharactersCut(Bitmap bm)
        {
            var recList = new List<Rectangle>();

            return recList;
        }

        /// <summary>
        /// 获取行高
        /// </summary>
        /// <param name="im">影像二值矩阵</param>
        /// <returns>行高</returns>
        private static int getLineHeight(byte[,] im)
        {
            var lh = 0;

            var w = im.GetLength(0);
            var h = im.GetLength(1);
            var gs = Projection.HorizontalProjection(im);

            //找到范围高度
            var tempInt = new int[2];
            for (int j = 0; j < h; j++)
            {
                if (gs[0, j] == 1)
                {
                    tempInt[0] = j;
                    break;
                }
            }
            for (int j = 0; j < h; j++)
            {
                if (gs[0, h - j - 1] == 1)
                {
                    tempInt[1] = h - j - 1;
                    break;
                }
            }
            var rh = tempInt[1] - tempInt[0] + 1;

            //找到每行投影宽
            var arr = new List<int[]>();
            for (int j = 0; j < h; j++)
            {
                var p = 0;
                for (int i = 0; i < w; i++)
                {
                    if (gs[i, j] == 1)
                    {
                        p++;
                    }
                    else
                    {
                        if (p != 0)
                        {
                            var intes = new int[2];
                            intes[0] = j;
                            intes[1] = p;
                            arr.Add(intes);
                        }
                        break;
                    }
                }
            }

            var tempRealHeight = new int[2];
            var count1 = 0;
            var count2 = 0;
            for (int i = 0; i < arr.Count - 1; i++)
            {
                if (arr[i+1][1] >= arr[i][1])
                {
                    count1++;
                    if (count1 == 3)
                    {
                        tempRealHeight[0] = arr[i -2][0];
                        break;
                    }
                }
                else
                {
                    count1 = 0;
                }
            }

            for (int i = arr.Count - 1; i > 0; i--)
            {
                if (arr[i][1] <= arr[i - 1][1])
                {
                    count2++;
                    if (count2 == 3)
                    {
                        tempRealHeight[1] = arr[i+2][0];
                        break;
                    }
                }
                else
                {
                    count2 = 0;
                }
            }

            if (tempRealHeight[1] - tempRealHeight[0] <= rh && tempRealHeight[1] - tempRealHeight[0] >= rh / 3 * 2)
            {
                lh = tempRealHeight[1] - tempRealHeight[0] + 1;
            }

            return lh;
        }
    }
}
